package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xpvideo.entity.LsDataCarousel;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.mapper.LsDataCarouselMapper;
import com.example.xpvideo.service.FileService;
import com.example.xpvideo.service.LsDataCarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 */
@Service
public class LsDataCarouselServiceImpl extends ServiceImpl<LsDataCarouselMapper, LsDataCarousel>
    implements LsDataCarouselService{
    @Autowired
    private FileService fileService;

    @Autowired
    private LsDataCarouselMapper dataCarouselMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class,RuntimeException.class})
    public ResponseResult addDataCarousel(LsDataCarousel dataCarousel, MultipartFile file) {
        if (null==file) {
            return ResponseResult.responseResult(ErrCodeEnum.unknown_10001,"请上传logo");
        }
        String url = fileService.addImages(file);
        dataCarousel.setLogo(url);
        int insert = dataCarouselMapper.insert(dataCarousel);
        if (insert > 0) {
            return ResponseResult.responseResult(0);

        }
        return ResponseResult.responseResult(ErrCodeEnum.unknown_20002, null);
    }

    // 根据id删除某条轮播文本
    @Override
    @Transactional(rollbackFor = {Exception.class,RuntimeException.class})
    public ResponseResult delDataCarousel(Integer id) {
        LsDataCarousel dataCarousel = dataCarouselMapper.selectById(id);
        if (null == dataCarousel) {
            return ResponseResult.responseResult(500, "该对象不存在");
        }
        // 删除oss文件
        boolean delImages = fileService.delImages(dataCarousel.getLogo());
        if (delImages) {
            dataCarouselMapper.deleteById(id);
            return ResponseResult.responseResult(0);
        }
        return ResponseResult.responseResult(ErrCodeEnum.unknown_9999, null);
    }

}




